package com.newly.center.business.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.newly.center.business.service.CheckRecordService;
import com.newly.center.business.service.DataScreenService;
import com.newly.center.business.service.MQTTDeviceService;
import com.newly.center.business.service.MediaService;
import com.newly.common.base.entity.business.Dto.CheckRecordDto;
import com.newly.common.base.entity.business.Po.CheckRecord;
import com.newly.common.base.entity.business.Po.Site;
import com.newly.common.base.entity.business.Vo.*;
import com.newly.common.core.utils.CopyUtil;
import com.newly.common.mapper.business.SiteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @anthor sbq
 * @date 2021/11/10 16:15
 */
@Service("dataScreenService")
public class DataScreenServiceImpl implements DataScreenService {

    @Autowired
    private SiteMapper siteMapper;
    @Autowired
    private CheckRecordService checkRecordService;
    @Autowired
    private MediaService mediaService;
    @Autowired
    private MQTTDeviceService deviceService;

    @Override
    public MapVo map(List<CheckRecordVo> checkRecordVos) {

        MapVo mapVo = new MapVo();
        List<MapSiteVo> mapSiteVos = new ArrayList<>();
        //今日超重数
        int todayOverweightNum = 0;
        //今日检测总数
        int todayCheckNum = 0;
        //今日超重百分比
        double ratio = 0.00;
        //站点总数
        Integer siteNum;
        List<Site> siteList = this.siteMapper.selectList(new QueryWrapper<Site>().lambda().eq(Site::getIsDeleted,0).orderByAsc(Site::getId));
        siteNum = siteList.size();
        for (Site site : siteList){
            MapSiteVo mapSiteVo = new MapSiteVo();
            mapSiteVo.setSiteId(site.getId().toString());
            mapSiteVo.setSiteName(site.getName());
            mapSiteVo.setLatitude(site.getLatitude());
            mapSiteVo.setLongitude(site.getLongitude());
            mapSiteVo.setTodayOverweightNum(0);
            mapSiteVo.setTodayTotalNum(0);
            mapSiteVo.setUrls(deviceService.getUrlBySiteId(site.getId().toString()));
            mapSiteVos.add(mapSiteVo);
        }
        mapVo.setSiteNum(siteNum);
        mapVo.setTodayTotalNum(todayCheckNum);
        mapVo.setTodayOverweightNum(todayOverweightNum);
        mapVo.setTodayOverweightPercent(ratio);
        mapVo.setMapSiteVoList(mapSiteVos);
        mapVo.setSiteNum(siteNum);
        if(ObjectUtil.isEmpty(checkRecordVos))
            return mapVo;
        //修改日期为yyyy-MM-dd格式
        checkRecordVos = checkRecordVos.stream().peek(t -> {
            t.setCreateTime(new DateTime(t.getCreateTime()).toString(DatePattern.NORM_DATE_FORMAT));
        }).collect(Collectors.toList());
        //筛选今日的数据
        checkRecordVos = checkRecordVos.stream().filter(p -> p.getCreateTime().equals(DateUtil.date().toString(DatePattern.NORM_DATE_FORMAT))).collect(Collectors.toList());
        //今日超重数
        todayOverweightNum = (int) checkRecordVos.stream().filter(t -> t.getOverweightType() != 0).count();
        mapVo.setTodayOverweightNum(todayOverweightNum);
        //今日检测总数
        todayCheckNum = checkRecordVos.size();
        mapVo.setTodayTotalNum(todayCheckNum);
        if (todayCheckNum == 0)
            ratio = 0.00;
        else
            ratio = NumberUtil.div((double)todayOverweightNum,(double) todayCheckNum,4);
        //今日超重百分比
        mapVo.setTodayOverweightPercent(ratio);
        //按站点名称分组
        Map<String,List<CheckRecordVo>> map = checkRecordVos.stream().collect(Collectors.groupingBy(CheckRecordVo::getSiteName));
        for (Map.Entry<String,List<CheckRecordVo>> entry : map.entrySet()){
            mapSiteVos.forEach(p -> {
                if (p.getSiteName().equals(entry.getKey())){
                    p.setTodayTotalNum(entry.getValue().size());
                    p.setTodayOverweightNum((int)entry.getValue().stream().filter(t -> t.getOverweightType() != 0).count());
                }
            });
        }
        return mapVo;
    }

    @Override
    public DataScreenVo count(Integer offset) {

        DataScreenVo dataScreenVo = new DataScreenVo();
        //排行
        List<LastXDayRankingVo> rankingVos = new ArrayList<>();
        //超重次数统计
        List<LastXDayOverweightVo> overweightVos = new ArrayList<>();
        //走势
        List<LastXDayTrendVo> trendVos = new ArrayList<>();
        //大屏地图-站点vo
        List<MapSiteVo> mapSiteVoList = new ArrayList<>();
        //地图
        MapVo mapVo = new MapVo();
        mapVo.setMapSiteVoList(mapSiteVoList);
        //轴数超重统计
        List<AxleCountVo> axleCountVos = new ArrayList<>();
        //车辆超重次数排行
        List<OverweightRankingVo> overweightRankingVos = new ArrayList<>();
        //24小时超重抓拍图片路径
        List<String> pictureUrls = new ArrayList<>();
        //实时数据
        //List<RealTimeVo> realTimeVos = new ArrayList<>();
        dataScreenVo.setRankingVos(rankingVos).setOverweightVos(overweightVos)
                .setTrendVos(trendVos)
                .setMapVo(mapVo)
                .setAxleCountVos(axleCountVos)
                .setOverweightRankingVos(overweightRankingVos)
                //.setRealTimeVos(realTimeVos)
                .setPictureUrls(pictureUrls);
        String startTime = DateUtil.offsetDay(DateUtil.date(),-(offset - 1)).toString();
        CheckRecordDto checkRecordDto = new CheckRecordDto().setStartTime(startTime).setEndTime(DateUtil.now()).setFlag(false);
        List<CheckRecord> checkRecordList = this.checkRecordService.getListByCondition(checkRecordDto);
        if (ObjectUtil.isEmpty(checkRecordList))
            return dataScreenVo;
        List<CheckRecordVo> checkRecordVos = CopyUtil.copyList(checkRecordList,CheckRecordVo.class);
        checkRecordVos = this.checkRecordService.cover(checkRecordVos);
        trendVos = this.checkRecordService.trend(checkRecordVos,offset);
        dataScreenVo.setTrendVos(trendVos);
        overweightVos = this.checkRecordService.Overweight(checkRecordVos,offset);
        dataScreenVo.setOverweightVos(overweightVos);
        axleCountVos = this.checkRecordService.axleCount(checkRecordVos);
        dataScreenVo.setAxleCountVos(axleCountVos);
        overweightRankingVos = this.checkRecordService.getOverweightRanking(checkRecordVos);
        dataScreenVo.setOverweightRankingVos(overweightRankingVos);
        rankingVos = this.checkRecordService.Ranking(checkRecordVos);
        dataScreenVo.setRankingVos(rankingVos);
        dataScreenVo.setPictureUrls(this.mediaService.getPictureUrls());
        dataScreenVo.setMapVo(this.map(checkRecordVos));
        //realTimeVos = this.checkRecordService.getRealTimeData();
        //dataScreenVo.setRealTimeVos(realTimeVos);
        return dataScreenVo;
    }
}
